package com.hangzhou.xyj.ui.activity;

import android.content.Intent;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.hangzhou.xyj.R;
import com.hangzhou.xyj.XYJApplication;
import com.hangzhou.xyj.manager.IntentManager;
import com.hangzhou.xyj.model.GroupMainModel;
import com.hangzhou.xyj.net.handler.GetGroupMainHandler;
import com.hangzhou.xyj.net.http.HttpLoader;
import com.hangzhou.xyj.net.request.GetGroupMainReq;
import com.hangzhou.xyj.ui.activity.base.BaseActivity;
import com.hangzhou.xyj.ui.adapter.GroupHeadersAdapter;
import com.hangzhou.xyj.ui.widget.CommonTitleBarView;
import com.hangzhou.xyj.ui.widget.stickyheadersrecyclerview.StickyRecyclerHeadersDecoration;
import com.hangzhou.xyj.ui.widget.stickyheadersrecyclerview.StickyRecyclerHeadersTouchListener;
import com.hangzhou.xyj.utils.constants.DataKey;

/**
 * Created by neo.duan on 2016/1/6.
 * 圈子
 */
public class GroupActivity extends BaseActivity {
    private RecyclerView mRecyclerView;
    private StickyRecyclerHeadersDecoration mHeadersDecoration;
    private GroupHeadersAdapter mAdapter;

    @Override
    public void initTop() {
        enableTitle(true, "圈子");
        enableRightNavTwoImg(true, getResources().getDrawable(R.drawable.ic_search), getResources().getDrawable(R.drawable.ic_publish));
        setOnNavRightListener(new CommonTitleBarView.OnNavRightListener() {
            @Override
            public void onNavRight() {
                if (!isLogin()) {
                    return;
                }
                IntentManager.getInstance().goAddGroupActivity(mContext);
            }
        });
        setOnNavRightLeftImgListener(new CommonTitleBarView.OnNavRightLeftImgListener() {
            @Override
            public void OnNavRightLeftImg() {
                IntentManager.getInstance().goSearchActivity(mContext);
            }
        });
    }

    @Override
    public void initLayouts() {
        setContentView(R.layout.activity_group);
    }

    @Override
    public void initViews() {
        mRecyclerView = findView(R.id.rv_group);
        //设置水平布局
        mRecyclerView.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false));
        // 设置item动画
        mRecyclerView.setItemAnimator(new DefaultItemAnimator());
        mRecyclerView.setAdapter(mAdapter = new GroupHeadersAdapter(mContext, new GroupMainModel()));

        // Add the sticky headers decoration
        mHeadersDecoration = new StickyRecyclerHeadersDecoration(mAdapter);
        mRecyclerView.addItemDecoration(mHeadersDecoration);

        //注册一个适配器数据观察者，当数据发生变化时，刷新
        mAdapter.registerAdapterDataObserver(new RecyclerView.AdapterDataObserver() {
            @Override
            public void onChanged() {
                mHeadersDecoration.invalidateHeaders();
            }
        });
    }

    @Override
    public void initDatas() {
        getGroupMain();
    }

    @Override
    public void initListeners() {
        StickyRecyclerHeadersTouchListener listener = new StickyRecyclerHeadersTouchListener(mRecyclerView, mHeadersDecoration);
        listener.setOnHeaderClickListener(new StickyRecyclerHeadersTouchListener.OnHeaderClickListener() {
            @Override
            public void onHeaderClick(View header, int position, long headerId) {
                Intent intent = new Intent(mContext, MoreGroupActivity.class);
                switch (position) {
                    case 0://新兴圈子
                        intent.putExtra(DataKey.INTENT_TAG, MoreGroupActivity.INTENT_TAG_NEW);
                        break;
                    case 1://名家部落
                        intent.putExtra(DataKey.INTENT_TAG, MoreGroupActivity.INTENT_TAG_FAMOUS);
                        break;
                    case 2://推荐圈子
                        intent.putExtra(DataKey.INTENT_TAG, MoreGroupActivity.INTENT_TAG_RECOMMEND);
                        break;
                    case 3://附近圈子
                        intent.putExtra(DataKey.INTENT_TAG, MoreGroupActivity.INTENT_TAG_NEAR);
                        break;
                }
                IntentManager.getInstance().goActivity(mContext, intent);
            }
        });
        mRecyclerView.addOnItemTouchListener(listener);
    }

    /**
     * 请求数据
     *
     * @param
     * @return
     * @author yaohuix
     * @date 2016/3/15 13:41
     */
    public void getGroupMain() {
        //向服务器请求数据
        GetGroupMainReq getGroupMainReq = new GetGroupMainReq(XYJApplication.getCity());
        HttpLoader.getInstance().sendRequest(new GetGroupMainHandler(getGroupMainReq, new MyHttpResponseListener() {
            @Override
            public void onSuccess(Object model) {
                super.onSuccess(model);
                GroupMainModel data = (GroupMainModel) model;
                mAdapter.update(data);
            }
        }, 0));
    }

}
